package com.transcargo.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.errors.ErrorList;

public class DelMailAction extends AuthAction {
	private String id = "";
	ArrayList<String> list_id = null;

    private void servletGetValues(HttpServletRequest request) throws ServletParametersException, ServletActionException {
		id = request.getParameter("id");
		if (id == null) {
			list_id = new ArrayList<String>();
			list = null;
			Map map = request.getParameterMap();
			Set set = map.keySet();
			Iterator ir = set.iterator();
			System.out.println("length=" + request.getContentLength());
			try {
				list = dbase.execute("SELECT id FROM mail WHERE (owner mod 100000000)=" + User_Id);
				iter = list.iterator();
				while (iter.hasNext()) {
					String[] res = iter.next();
					String p = "id" + res[0];
					String val = request.getParameter(p);
					if (val != null && val.equals("on")) list_id.add(res[0]);
					System.out.println(p + ", value=" + val);
				}
			} catch (dbException e) {
				System.out.println("Database exception: " + e.getMessage());
				throw new ServletActionException();
			}
		}
    }
    private void servletValidateValues() throws ServletValidationException {
    	
    }
    private void dropMail(String mail_id) throws dbException {
		list = dbase.execute("SELECT body_id FROM mail WHERE id=" + mail_id);
		iter = list.iterator();
		String body_id = "";
		if (iter.hasNext()) {
			String[] res = iter.next();
			body_id = res[0];
		}
		dbase.executeUpdate("DELETE FROM mail WHERE id=" + mail_id + " AND owner=" + User_Id);
		list = dbase.execute("SELECT count(id) FROM mail WHERE body_id=" + body_id);
		iter = list.iterator();
		String[] rec = iter.next();
		if (rec[0].equals("0")) {
			dbase.executeUpdate("DELETE FROM mail_body WHERE id=" + body_id);
		}
    	
    }
    private void servletExecuteAction() throws ServletActionException {
        try {
        	if (id != null) {
        		int Id = Integer.parseInt(id);
        		dropMail(Integer.toString(Id));
        	} else {
        		Iterator<String> iterator = list_id.iterator();
        		while (iterator.hasNext()) {
        			String mail_id = iterator.next(); 
        			dropMail(mail_id);
        		}
        	}
        } catch (dbException e) {
			System.out.println("Database exception: " + e.getMessage());
			throw new ServletActionException();
		}
    }
    public void processAction(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
            servletGetValues(request);
            servletValidateValues();
            servletExecuteAction();
            sendRedirect(response, "mail.jsp");
	    } catch (ServletParametersException e) {
            if (e.getMessage() != null) {
                sendError(response, new Exception(e.getMessage()));
            } else {
                sendRedirect(response, "mail.jsp");
            }
        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletActionException e) {
            sendError(response, new Exception("" + ErrorList.ERROR_STR_GENERIC + dbase.getError()));
        }
    }
}
